<?php
/*
 *	GetSiteConfig.php
 *	public 
 *	@param GetUrl
 *			@@param string $type list/item
 *			@@return string url
 *	@param $IsFix boolen Remove the invalid value in array or not.
 *	@param $ApiListUrl string url
 *	@param $ApiListParam string queryparam
 *	@param $ApiItemUrl string url
 *	@param $ApiItemParam string queryparam
 *	@param $ApiCategory array ApiCategory
 *	@param $ApiSortOrder array ApiSortOrder
 *	@param $ApiListModel array List Model
 *	@param $ApiItemModel array Item Model
 */
//common
require_once(DATA_PATH."table_dao.php");
include_once(CLASS_PATH."Transform.php");
require_once(CLASS_PATH."Google.Api.php");
class GetSiteConfig{
	private $IsFix = true;
	private $ApiListUrl = NULL;
	private $ApiListParam = array();
	private $ApiItemUrl = NULL;
	private $ApiItemParam = array();
	
	
	public $IsLoadCongig = false;
	public $IsGetApiPage = true;
	public $ApiXmlOrJson = "JSON";//JSON/JSONP/XML    [JSONP == callback(JSON);]
	public $ApiCategory = array();

	public $ApiSortOrder = array();

	public $ApiListModel =array();

	public $ApiItemModel = array();

	public function GetSiteConfig($type,$site,$request=array(),$is_tran=false,$is_single_site=true){
    	$client		= checknull("client", $request,"pc");
    	$keyword	= checknull("q", $request);
    	
		//config old search file
		include(CONFIG_PATH."SiteCateArray.php");
		$site_file = CONFIG_API_SITES_PATH.$type.".".$site.".php";
		if(file_exists($site_file)){
			//google
			$Google = new GoogleApi();
			$tran = $Google->Get($keyword);
			if($is_tran){
				$keyword = checknull("ja", $tran);
			}
			//config
			$pagenum = _GET("p");
			      
			include($site_file);//テスト
			if($pagenum>$APIMaxPage){
				$this->IsGetApiPage = false;
			}
			$this->IsLoadCongig = true;
			//Set Api Model.
			$this->IsFix = empty($isfix)?true:$isfix;
			$this->ApiXmlOrJson = $XmlOrJson;
			$this->Fixmodel($type,$client,$ListModel,$ItemModel,$Category,$SortOrder);
			//Set const config.
			$this->ApiListUrl = $ListUrl;
			$this->ApiItemUrl = $ItemUrl;
			//Set $_GET into config.
			$this->ApiItemParam = $ItemParam;
			$this->ApiListParam = $ListParam;
			if(false==$is_single_site){
          $categorykey = $this->findkey();
          if($categorykey){
              $this->ApiListParam[$categorykey] = $this->ApiListParam[$categorykey."_all"];
          }
			}
			//insert keyword
			$sType = ($type=="auction")?"オークション":"ショッピング";
			$fixcategory = checknull("id", checknull(_GET("c"), $SiteCate[$type][$site]));
			$keyword = trim($keyword);
			$data = array(
				"sType"		=> $sType,
				//"Category"	=> empty($this->ApiListParam["category"])?"ALL":$this->ApiListParam["category"],
				"Category"	=> empty($fixcategory)?"ALL":$fixcategory,
				"Lang"		=> "cn",
				"Key"		=> $keyword ,
				"jp"		=> checknull("ja", $tran),
				"cn"		=> checknull("zh-CN", $tran),
				"tw"		=> checknull("zh-TW", $tran),
				"en"		=> checknull("en", $tran)
			);
			if($keyword!=""&&$keyword!=NULL){
				$key_dao = new table_dao("keywords");
				$key_Search = array(
						"Key"	=> $keyword,
					);
				//$keydata = $key_dao->get("Key","'$keyword'");
				$keydata = $key_dao->get($key_Search);
				
				if(is_array($keydata)){
					$keydata["0"] = checknull("0", $keydata);
					$oldhits = checknull("hits", $keydata["0"]);
					$data["hits"] = $oldhits+1;
					if(empty($oldhits)){
						$data["handleFlag"] = "1";
					}
					$key_dao->updata(checknull("Id", $keydata["0"]),$data);
				}else{
					$data["hits"]="1";
					$data["createDate"]="NOW( ) ";
					$data["handleFlag"]="0";
					$key_dao->add($data);
				}
			}
		}
	}
	private function findkey(){
		if(is_array($this->ApiListParam)){
			foreach($this->ApiListParam as $key=>$val){
				$pos = strrpos($key , "_all");
				if ($pos ===false) { // note: three equal signs
					continue;
				}else{
					return str_replace("_all","",$key);
					break;
				}
			}
		}
		return false;
	}
	/**
	 * Get Api Url.
	 * @param string $type list/item
	 * @return string url
	 */
	public function GetUrl($type = "list"){
		$result = NULL;
		if($this->IsLoadCongig){
			if($type=="list"){
				$param=$this->ApiListParam;
				$url=$this->ApiListUrl;
			}
			else{
				$param=$this->ApiItemParam;
				$url=$this->ApiItemUrl;
			}

			if($this->IsFix){$param = Transform::GetRequest($param);}
			$Query = Transform::GetQuery($param);
			$result = $url.$Query;
		}
		return $result;
	}
	private function Fixmodel($type,$client="pc",$ListModel,$ItemModel,$Category,$SortOrder){
		$carrier_model = "model.pc.php";
		if($client=="mobile"){
			$carrier_model = "model.iphone.php";
		}
		include(CONFIG_API_MODEL_PATH."model.common.php");
		include(CONFIG_API_MODEL_PATH.$carrier_model);
		if($type=="shopping"){
			$Model_SortOrder = $Model_SortOrder_Shopping;
		}else{
			$Model_SortOrder = $Model_SortOrder_Auction;
		}
		
		$this->ApiCategory = $this->FixApiModel($Model_Category,$Category);
		$this->ApiSortOrder = $this->FixApiModel($Model_SortOrder,$SortOrder);
		
		
		$this->ApiListModel = $this->FixApiModel($Model_ListModel,$ListModel);
		$this->ApiItemModel = $this->FixApiModel($Model_ItemModel,$ItemModel);
	}
	private function FixApiModel($model,$input){
		if(is_array($model)){
			foreach($model as $key=>$val){
				if(is_array($val)){
					$keysA = array_keys($model[$key]);
					$keysB = array_keys(checknull($key, $input));
					foreach($keysA as $keyAskey=>$keyAsval){
						$result[$key][$keysB[$keyAskey]] = checknull($keyAsval, $val);
					}
				}else{
					$result[$key] = $val;
				}
			}
		}
		$modelresult = $this->Getmodelvalue($result,$input);
		return $modelresult;
	}
	private function Getmodelvalue($model,$input){
		if(is_array($model)){
			$result = $model;
			foreach($model as $key=>$val){
				$result[$key] = $this->Getmodelvalue($val,checknull($key, $input));
			}
		}else{$result = $input;}
		return $result;
	}
}